/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package io.renren.modules.sys.controller;

import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.annotation.SysLog;
import io.renren.common.exception.RRException;
import io.renren.common.utils.Constant;
import io.renren.common.vo.NavVo;
import io.renren.common.vo.RV;
import io.renren.modules.sys.entity.Menu;
import io.renren.modules.sys.service.MenuService;
import io.renren.modules.sys.service.ShiroService;
import io.renren.modules.sys.vo.MenuTree;

/**
 * 系统菜单
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年10月27日 下午9:58:15
 */
@RestController
@RequestMapping("/sys/menu")
public class MenuController extends AbstractController {
	@Autowired
	private MenuService menuService;
	@Autowired
	private ShiroService shiroService;

	/**
	 * 导航菜单
	 */
	@GetMapping("/nav")
	public RV nav(){
		List<Menu> menuList = menuService.getUserMenuList(getUserId());
		Set<String> permissions = shiroService.getUserPermissions(getUserId());
		NavVo  nav = new NavVo(); 
		nav.setMenuList(menuList);
		nav.setPermissions(permissions);
		return RV.ok(nav);
	}
	
	/**
	 * 所有菜单列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("sys:menu:list")
	public RV menuList(){
		
		
		List<Menu> menuList = menuService.selectListByUser(getUser());
		
		return RV.ok(menuList);
	}
	
	/**
	 * 所有菜单树
	 */
	@GetMapping("/menuTree")
//	@RequiresPermissions("sys:menu:tree")
	public RV menuTree(){
			
		List<MenuTree> menuList = menuService.selectMenuTreeByUser(getUser());
		
		return RV.ok(menuList);
	}
	
	/**
	 * 选择菜单(添加、修改菜单)
	 */
	@GetMapping("/select")
	@RequiresPermissions("sys:menu:select")
	public RV select(){
		//查询列表数据
		List<MenuTree> menuList = menuService.queryNotButtonTree();
		
		//添加顶级菜单
//		Menu root = new Menu();
//		root.setMenuId("0");
//		root.setName("一级菜单");
//		root.setParentId("-1");
//		root.setOpen(true);
//		menuList.add(root);
		
		return RV.ok(menuList);
	}
	
	/**
	 * 菜单信息
	 */
	@GetMapping("/info/{menuId}")
	@RequiresPermissions("sys:menu:info")
	public RV info(@PathVariable("menuId") Long menuId){
		Menu menu = menuService.selectById(menuId);
		return RV.ok(menu);
	}
	
	/**
	 * 保存
	 */
	@SysLog("保存菜单")
	@PostMapping("/save")
	@RequiresPermissions("sys:menu:save")
	public RV save(@RequestBody Menu menu){
		//数据校验
		verifyForm(menu);
		
//		menuService.insert(menu);
		
		
		return RV.ok(menuService.saveMenu(menu));
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改菜单")
	@PostMapping("/update")
	@RequiresPermissions("sys:menu:update")
	public RV update(@RequestBody Menu menu){
		//数据校验
		verifyForm(menu);
				
		menuService.updateById(menu);
		
		return RV.ok();
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除菜单")
	@PostMapping("/delete/{menuId}")
	@RequiresPermissions("sys:menu:delete")
	public RV delete(@PathVariable("menuId") String menuId){
		// TODO 此处逻辑不严谨 
//		if(menuId <= 31){
//			return RV.fail("系统菜单，不能删除");
//		}

		//判断是否有子菜单或按钮
		List<Menu> menuList = menuService.queryListParentId(menuId);
		if(menuList.size() > 0){
			return RV.fail("请先删除子菜单或按钮");
		}

		menuService.delete(menuId);

		return RV.ok();
	}
	
	/**
	 * 验证参数是否正确
	 */
	private void verifyForm(Menu menu){
		if(StringUtils.isBlank(menu.getName())){
			throw new RRException("菜单名称不能为空");
		}
		
		if(menu.getParentId() == null){
			throw new RRException("上级菜单不能为空");
		}
		
		//菜单
		if(menu.getType() == Constant.MenuType.MENU.getValue()){
			if(StringUtils.isBlank(menu.getUrl())){
				throw new RRException("菜单URL不能为空");
			}
		}
		
		//上级菜单类型
		int parentType = Constant.MenuType.CATALOG.getValue();
		if(!menu.getParentId().equals("0")){
			Menu parentMenu = menuService.selectById(menu.getParentId());
			parentType = parentMenu.getType();
		}
		
		//目录、菜单
		if(menu.getType() == Constant.MenuType.CATALOG.getValue() ||
				menu.getType() == Constant.MenuType.MENU.getValue()){
			if(parentType != Constant.MenuType.CATALOG.getValue()){
				throw new RRException("上级菜单只能为目录类型");
			}
			return ;
		}
		
		//按钮
		if(menu.getType() == Constant.MenuType.BUTTON.getValue()){
			if(parentType != Constant.MenuType.MENU.getValue()){
				throw new RRException("上级菜单只能为菜单类型");
			}
			return ;
		}
	}
}
